package GUILayer;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.* ;
import java.util.*;
import ControlLayer.*;



/**
 * Write a description of class UILoan here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class UILoan
{
    private  JTextField idDVDText=new JTextField(10);
    private  JTextField serialCopyText = new JTextField(10);
    private  JTextField idPersonDeleteText = new JTextField(10);
    private  JTextField deleteCopySerialText=new JTextField(10);
    private  JTextField deleteDVDIDText=new JTextField(10);
    private JTextField periodText=new JTextField(10);
    private JTextField personIDText=new JTextField(10);
    private JTextField loanIDText=new JTextField(10);
    
    private DVDCtr dvdController;
    private LoanListController loanController;
    private AddressCtr addressController;

    /**
     * Constructor for objects of class UILoan
     */
    public UILoan()
    {
       dvdController=new DVDCtr();
       loanController=new LoanListController();
       addressController=new AddressCtr();
    }
    
    public JPanel getLoansPanel(JFrame frame)
    {
        JPanel panelMain=new JPanel();
        panelMain.setLayout(new BoxLayout(panelMain, BoxLayout.Y_AXIS));
        
        JButton addLoan=new JButton("Add Loan");
        JButton deleteLoan=new JButton("Delete Loan");
        JButton showLoan=new JButton("Show Loan");
        
        JLabel idDVDLabel= new JLabel("              DVD ID  ");
        JLabel deleteDVDIDLabel=new JLabel("              DVD  ID   ");
        JLabel loanIDLabel= new JLabel("            Loan  ID        ");
        JLabel personIDLabel=new JLabel("                Person ID");
        JLabel deleteCopySerialLabel= new JLabel("Copy S. Number");
        JLabel personDeleteLabel=new JLabel("       Person ID    ");
        JLabel serialCopyLabel= new JLabel("Copy S. Number");
        JLabel periodLabel= new JLabel("                    Period");
        

        final JFrame frame1=frame;
        addLoan.addActionListener(new ActionListener() {
                               public void actionPerformed(ActionEvent e) { addLoan(frame1); }
                           });  
        deleteLoan.addActionListener(new ActionListener() {
                               public void actionPerformed(ActionEvent e) { deleteLoan(frame1); }
                           });         
         showLoan.addActionListener(new ActionListener() {
                               public void actionPerformed(ActionEvent e) { showLoan(frame1); }
                           });         

        

        
        JPanel idDVDPanel=new JPanel();
        idDVDPanel.add(idDVDLabel);
        idDVDPanel.add(idDVDText);
        addComponent(idDVDPanel,panelMain);
        
        JPanel panelSerial=new JPanel();
        panelSerial.add(serialCopyLabel);
        panelSerial.add(serialCopyText);
        addComponent(panelSerial,panelMain);
        
        JPanel panelPersonID=new JPanel();
        panelPersonID.add(personIDLabel);
        panelPersonID.add(personIDText);
        addComponent(panelPersonID,panelMain);
        
        JPanel panelLoanID=new JPanel();
        panelLoanID.add(loanIDLabel);
        panelLoanID.add(loanIDText);
        addComponent(panelLoanID,panelMain);
        
        JPanel panelPeriod=new JPanel();
        panelPeriod.add(periodLabel);
        panelPeriod.add(periodText);
        addComponent(panelPeriod,panelMain);
        
                panelMain.add(Box.createRigidArea(new Dimension(300,5)));
        addComponent(addLoan,panelMain);
           panelMain.add(Box.createRigidArea(new Dimension(300,15)));
        

        
        JPanel panelToDeleteDVDID=new JPanel();
        panelToDeleteDVDID.add(deleteDVDIDLabel);
        panelToDeleteDVDID.add(deleteDVDIDText);
        addComponent(panelToDeleteDVDID,panelMain);
        
        JPanel panelDeleteCopySerial=new JPanel();
        panelDeleteCopySerial.add(deleteCopySerialLabel);
        panelDeleteCopySerial.add(deleteCopySerialText);
        addComponent(panelDeleteCopySerial,panelMain);
        
        JPanel panelDeleteLoanPersonID=new JPanel();
        panelDeleteLoanPersonID.add(personDeleteLabel);
        panelDeleteLoanPersonID.add(idPersonDeleteText);
        addComponent(panelDeleteLoanPersonID,panelMain);
        
        
        
          
       JPanel panelOptionButtons=new JPanel(); 
       panelOptionButtons.add(deleteLoan);
       panelOptionButtons.add(showLoan);
        addComponent(panelOptionButtons,panelMain);
        panelMain.add(Box.createRigidArea(new Dimension(300,5)));
        
        
         
         return panelMain;    
    }
    
    private void addLoan(JFrame frame)
    {
        try
          {
           long idDVD=Long.valueOf(idDVDText.getText()) ;
           long serialCopy=Long.valueOf(serialCopyText.getText());
           long idPerson=Long.valueOf(personIDText.getText());
           long idLoan=Long.valueOf(loanIDText.getText());
           int loanPeriod=Integer.valueOf(periodText.getText());
           
           if(dvdController.dvdExist(idDVD) && dvdController.copyExist(idDVD,serialCopy) && 
                   addressController.personExist(idPerson))
            {
              loanController.createLoan(idLoan,loanPeriod,idPerson,idDVD,serialCopy);
              idDVDText.setText("");
              serialCopyText.setText("");
              personIDText.setText("");
              loanIDText.setText("");
              periodText.setText("");
       
          
            JOptionPane.showMessageDialog(frame,"Loan added successfuly.");
          }
          
          else 
             JOptionPane.showMessageDialog(frame,"One of the IDs is not correct,please try again!.","",JOptionPane.ERROR_MESSAGE);
        }
        
          catch(Exception e)
          {
            JOptionPane.showMessageDialog(frame,"Error while adding loan,please try again!.","",JOptionPane.ERROR_MESSAGE);
            }
    
     }
    
    private void deleteLoan(JFrame frame)
    {
         try
        {
           long idDVD=Long.valueOf(deleteDVDIDText.getText()) ;
           long serialCopy=Long.valueOf(deleteCopySerialText.getText());
           long idPerson=Long.valueOf(idPersonDeleteText.getText()); 
            long idLoan=loanController.getLoanID(idPerson,idDVD,serialCopy);
           
         int choice=JOptionPane.showConfirmDialog(frame,"Do you really want to delete this Copy?", "", JOptionPane.YES_NO_OPTION);
           if(choice==0)
           {
            JTextField days = new JTextField();
            JLabel message=new JLabel("Enter for how many days the DVD was taken:");
            final JComponent[] inputs = new JComponent[] {message,days};
            JOptionPane.showMessageDialog(frame, inputs,"", JOptionPane.QUESTION_MESSAGE);
            int numberOfDays=Integer.valueOf(days.getText());
            loanController.updateDVDState(idLoan,numberOfDays);
            
            String state=loanController.getDVDState(idLoan);
            loanController.DVDisReturned(idPerson,idDVD,serialCopy);
            
            JOptionPane.showMessageDialog(frame,"Loan deleted successfuly.State: "+state);

            deleteDVDIDText.setText("");  
            deleteCopySerialText.setText("");  
            idPersonDeleteText.setText(""); 
          
           }
        }
        
        catch(Exception e)
        {
          JOptionPane.showMessageDialog(frame,"One of the IDs is not correct,please try again!.","",JOptionPane.ERROR_MESSAGE);
        }

    }
    
    
    private void showLoan(JFrame frame)
    {
       
        try
        {
           long idDVD=Long.valueOf(deleteDVDIDText.getText()) ;
           long serialCopy=Long.valueOf(deleteCopySerialText.getText());
           long idPerson=Long.valueOf(idPersonDeleteText.getText()); 
           long idLoan=loanController.getLoanID(idPerson,idDVD,serialCopy);
           
          String[] columnNames = {"Borrow Date","Period","State"};
          Object [][] data={ {loanController.getDVDBorrowDate(idLoan),loanController.getDVDPeriod(idLoan)
            ,loanController.getDVDState(idLoan)}};
   
          deleteDVDIDText.setText("");  
          deleteCopySerialText.setText("");  
          idPersonDeleteText.setText(""); 
          
        JTable table = new JTable(data, columnNames);
        JFrame inFrame=new JFrame();
        inFrame.setLayout(new BorderLayout());
        inFrame.add(table.getTableHeader(), BorderLayout.PAGE_START);
        inFrame.add(table, BorderLayout.CENTER);
        
        inFrame.pack();
        inFrame.setVisible(true);
       }
       
       
       
       catch(Exception e)
       {
        JOptionPane.showMessageDialog(frame,"Wrong IDs,please try again!.","",JOptionPane.ERROR_MESSAGE);
       }

    }

     private static void addComponent(JComponent component, Container container)
    {
        component.setAlignmentX(Component.CENTER_ALIGNMENT);
        container.add(component);
        
    }    
}
